<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/8/29
 * Time: 21:06
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Auth as AuthModel;
use think\facade\Request;


class Auth extends AdminBase
{
    public function index()
    {
        if(Request::isAjax()){
            $res = null;
            $type = Request::param('type/s');
            $authlist = AuthModel::instance()->getAllAuths();
            switch ($type)
            {
                case 'tree':
                    $authlist = AuthModel::instance()->getTreeMenu($authlist,'');
                    $res = json(['code'=>0,'msg'=>'数据列表','data'=>$authlist]);
                    break;
                case 'list':
                    $data = AuthModel::instance()->getAllAuths();
                    $res = json(['code'=>0,'msg'=>'数据列表','data'=>$data]);
                    break;
            }
            return $res;
        }
        else{
            return $this->fetch();
        }
    }

    public function add(){
        if(Request::isPost())
        {
            $form = Request::param('form/a');
            $form['guid'] = guid();
            AuthModel::instance()->insert($form);
            $this->success('成功','');
        }else{
            $this->assign('parent',array());
            $this->assign('model',array());
            return $this->fetch('auth');
        }
    }

    public function edit(){
        if(Request::isPost())
        {
            $form = Request::param('form/a');
            AuthModel::instance()->where(['guid'=>$form['guid']])->update($form);
            $this->success('修改成功','','');
        }else{
            $guid = Request::param('id');
            $model = AuthModel::instance()->where(['guid'=>$guid])->findOrEmpty();
            $parent = AuthModel::instance()->where(['guid'=>$model['pid']])->findOrEmpty();
            $this->assign('parent',$parent);
            $this->assign('model',$model);
            return $this->fetch('auth');
        }
    }

    function remove(){
        $id = Request::param('id/s');
        AuthModel::instance()->where(['guid'=>$id])->delete();
        $this->success('成功','','');
    }

}